package framework;

import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import framework.webclient.ProxyData;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

public class LoginPane extends Pane {
	private LoginHandler loginHandler;
	private PasswordField password;
	private TextField username;
	private TextField proxyDomain;
	private TextField proxyPcname;
	private TextField proxyUsername;
	private TextField proxyPassword;
	private TextField proxyServer;
	private TextField proxyPort;
	private CheckBox check;

	public LoginPane() {        
		VBox vbox = new VBox();
		this.getChildren().add(vbox);
		//vbox.getChildren().add(new Label("Google Login"));
		vbox.getChildren().add(createAccount());
		vbox.getChildren().add(createPassword());
		vbox.getChildren().add(createProxySetting());
		vbox.getChildren().add(createOkCancel());
	}
	
	private Pane createProxySetting() {
		VBox vbox = new VBox();
		proxyDomain = new TextField("main.intgin.net");
		try {
			proxyPcname = new TextField(InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		proxyUsername = new TextField();
		proxyPassword = new PasswordField();
		proxyServer = new TextField("http-proxy.noc.anritsu.co.jp");
		proxyPort = new TextField("8080");
		check = new CheckBox("Proxy");
		final VBox proxyPane = new VBox();
		vbox.getChildren().add(check);
		vbox.getChildren().add(proxyPane);
		proxyPane.getChildren().add(createText("Domain", proxyDomain));
		proxyPane.getChildren().add(createText("PC Name", proxyPcname));
		proxyPane.getChildren().add(createText("Username", proxyUsername));
		proxyPane.getChildren().add(createText("Password", proxyPassword));
		proxyPane.getChildren().add(createText("Server", proxyServer));
		proxyPane.getChildren().add(createText("Port", proxyPort));
		proxyPane.setVisible(false);
		check.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				proxyPane.setVisible(check.isSelected());
			}
		});
		return vbox;
	}

	private HBox createText(String text, TextField textField) {
		HBox hbox = new HBox();
		hbox.getChildren().add(new Label(text));
		hbox.getChildren().add(textField);
		return hbox;
	}

	private Node createOkCancel() {
		HBox hbox = new HBox();
		Button okButton = new Button("Login");
		hbox.getChildren().add(okButton);
		okButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				loginHandler.onOK(username.getText(), password.getText(), getProxyData());
			}
		});
		return hbox;
	}
	
	private ProxyData getProxyData() {
		if (!this.check.isSelected()) {
			return null;
		}
		ProxyData proxy = new ProxyData();
		proxy.domain = this.proxyDomain.getText();
		proxy.pcname = this.proxyPcname.getText();
		proxy.proxypassword = this.proxyPassword.getText();
		proxy.proxyport = Integer.valueOf(this.proxyPort.getText());
		proxy.proxyusername = this.proxyUsername.getText();
		proxy.proxyserver = this.proxyServer.getText();
		return proxy;
	}
	private Node createPassword() {
		HBox hbox = new HBox();
		hbox.getChildren().add(new Label("Password"));
		password = new PasswordField();
		password.setText("anritsutaro");
		hbox.getChildren().add(password);
		return hbox;
	}

	private Node createAccount() {
		HBox hbox = new HBox();
		hbox.getChildren().add(new Label("Google Account"));
		username = new TextField("anritsutaa@gmail.com");
		hbox.getChildren().add(username);
		return hbox;
	}
	
	public void setOnOk(LoginHandler handler) {
		this.loginHandler = handler;
	}
}
